package edu.wuster.blog.controller;


import edu.wuster.blog.entity.Message;
import edu.wuster.blog.entity.Result;
import edu.wuster.blog.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    //新增消息
    @PostMapping
    public Result addMessage(@RequestBody Message message) {
        log.info("新增消息请求");
        messageService.addMessage(message);
        log.info("新增消息成功");
        return Result.success();
    }

    //用户删除消息
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("用户删除消息(id：{})", id);
        messageService.deleteById(id);
        log.info("用户删除消息成功");
        return Result.success();
    }

    //用户查看消息
    @GetMapping("/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        log.info("用户id：{}查看消息", userId);
        List<Message> list = messageService.selectByUserId(userId);
        log.info("用户查看消息成功");
        return Result.success(list);
    }
}
